import { promiseRequest } from "./index";

interface FilterItem {
  key: string;
  item?: any[];
}

const getQueryString = (filter: FilterItem[]) => filter.map((e: FilterItem) => {
  if (e.key === 'categories' && e.item) {
    return `${e.key}=${e.item.join(',')}`
  }
  return e.item && e.item.length ? `${e.key}=${e.item.join(',')}` : `${e.key}=`
}).join("&");

const api = {
  getActivityList: async (page: number, size: number, filter = [], search = "") => {
    const res = await promiseRequest(`/post/activity/filter?page=${page}&size=${size}&${getQueryString(filter)}&query${search !== "" ? `=${search}` : ''}`, 'GET') as any
    const body = res["data"];
    const data = body["data"] as any[];
    return {
      total_page: body['total_page'] as number,
      current_page: body['current_page'] as number,
      has_next: body['has_next'] as boolean,
      data: data.map((item) => ({
        id: item['id'] as number,
        name: item['name'] as string,
        date_time: item['date_time'] as string,
        submission_deadline: item['submission_deadline'] as string,
        type_id: item['type_id'] as number,
        poster: item['poster'] as string,
        publisher_name: item['publisher_name'] as string,
        publisher_avatar: item['publisher_avatar'] as string,
      }))
    };
  },
  getActivityDetail: async (id: number) => {
    const res = await promiseRequest(`/post/activity/${id}/detail`, "GET") as any;
    const data = res["data"];
    return {
      id: data['id'] as number,
      name: data['name'] as string,
      activity_time: data['activity_time'] as string,
      additional_conditions: data['additional_conditions'] as string,
      limit: data['limit'] as string,
      deadline: data['deadline'] as string,
      location: data['location'] as string,
      type_id: data['type_id'] as number,
      poster: data['poster'] as string,
      publisher_name: data['publisher_name'] as string,
      publisher_avatar: data['publisher_avatar'] as string,
    }
  },
  getActivityPoster: async (id: number) => {
    return (await promiseRequest(`/post/activity/${id}/poster`, "GET", {
      toast: false,
    }))["data"];
  },
  getActivityContent: async (id: number) => {
    return (await promiseRequest(`/post/activity/${id}/content`, "GET"))["data"];
  },
  getAnnouncementList: async (page: number, size: number, filter = [], search = "") => {
    // console.log(`/post/announcement/filter?page=${page}&size=${size}&${getQueryString(filter)}&query${search !== "" ? `=${search}` : ''}`);
    const res = (await promiseRequest(`/post/announcement/filter?page=${page}&size=${size}&${getQueryString(filter)}&query${search !== "" ? `=${search}` : ''}`, 'GET')) as any;
    const body = res["data"];
    const data = body['data'] as any[];
    return {
      total_page: body['total_page'] as number,
      current_page: body['current_page'] as number,
      has_next: body['has_next'] as boolean,
      data: data.map((item) => ({
        id: item['id'] as number,
        name: item['name'] as string,
        date_time: item['date_time'] as string,
        type_id: item['type_id'] as number,
        poster: item['poster'] as string,
        publisher_name: item['publisher_name'] as string,
        publisher_avatar: item['publisher_avatar'] as string,
      }))
    };
  },
  getAnnouncementDetail: async (id: number) => {
    const res = await promiseRequest(`/post/announcement/${id}/detail`, "GET") as any;
    const data = res["data"];
    return {
      id: data['id'] as number,
      name: data['name'] as string,
      published_time: data['published_time'] as string,
      type: data['type'] as number,
      poster: data['poster'] as string,
      publisher_name: data['publisher_name'] as string,
      publisher_avatar: data['publisher_avatar'] as string,
    }
  },
  getAnnouncementContent: async (id: number) => {
    return (await promiseRequest(`/post/announcement/${id}/content`, "GET"))["data"];
  },
  getAnnouncementPoster: async (id: number) => {
    return (await promiseRequest(`/post/announcement/${id}/poster`, "GET", {
      toast: false,
    }))["data"];
  },
  getSuggestion: async () => {
    return (await promiseRequest(`/post/activity/suggestion`, 'GET'))["data"]["data"];
  },
}
export default api;
